<?
	session_start();
	if($_SESSION["ID_TIPUS"]!="1") header("Location: ./index.php");	
	include("./connection");
	$connection=mysql_connect($server, $user, $passwd);
	$db= mysql_select_db($name_db, $connection);
	
	if(!isset($_REQUEST["modifica"])) {
		$sql="UPDATE login SET email=`".$_REQUEST["email"]."`, nom=`".$_REQUEST["nom"]."`, cnom1=`".$_REQUEST["cnom1"]."`, cnom2=`".$_REQUEST["cnom2"]."`"_
		", id_tipus=`".$_REQUEST["grup"]."` WHERE id='".$_REQUEST["id"]."'";
		mysql_query($sql, $connection);
		header("Location: ./edit.php");
		mysql_close();
	}
	
	if(!isset($_REQUEST["drop"]) && $_REQUEST["drop"]=="1") {
		$sql="UPDATE login SET valid=`0` WHERE email='".$_REQUEST["euser"]."'";
		mysql_query($sql, $connection);
		header("Location: ./edit.php");
		mysql_close();
	}
	?>
<html>
<head>
	<title><? if (isset($_REQUEST["euser"])){ echo "Edicio d'usuari" } else { echo "Edicio del usuari ".$_REQUEST["euser"];}?></title>
</head>
<body>
<? if (isset($_REQUEST["euser"])){
	$sql="select id,email,nom,cnom1,cnom2 from login where valid='1'";
	$rs=mysql_query($sql, $connection) or die ("No s'ha obtingut cap camp");
	?><table border="1">
	<tr><td>Editar</td><td>Borrar</td><td>email</td><td>Nom</td><td>Primer cognom</td><td>Segon cognom</td></tr>
<?
	while($row=mysql_fetch_array($rs)) {
		echo "<tr><td><a href=\"./edit.php?euser=".$_row["id"]."&drop=0\"><img src=\"./img/b_edit.png\"></a></td>";
		echo "<td><a href=\"./edit.php?euser=".$_row["id"]."&drop=1\"><img src=\"./img/b_drop.png\"></a></td>";
		echo "<td>".$_row["email"]."</td><td>".$_row["nom"]."</td><td>".$_row["cnom1"]."</td><td>".$_row["cnom2"]."</td></tr>";
	}
?>
</table>
<? }
if(!isset($_REQUEST["euser"]) && $_REQUEST["drop"]=="0"){
	$sql="select * from login where id='".$_REQUEST["euser"]."'";
	$rs=mysql_query($sql, $connection) or die ("No s'ha obtingut cap camp");
	$row=mysql_fetch_array($rs);
?>
	<form accept="edit.php?id=<? echo $row["id"];?>" method="POST">
	email: <input type="text" name="email" <? echo "value=\".$row["email"]."\";?>>
	Nom: <input type="text" name="nom" <? echo "value=\".$row["nom"]."\";?>>
	Primer cognom: <input type="text" name="cnom1" <? echo "value=\".$row["cnom1"]."\";?>>
	Segon cognom: <input type="text" name="cnom2" <? echo "value=\".$row["cnom2"]."\";?>>
	<? if ($_SESSION["ID_TIPUS"]=="1"){?><select name="grup">
		<option value="1" <? if($row["id_tipus"]=="1"){ ?> selected="selected" <? }?>>Administradors</option>
		<option value="2" <? if($row["id_tipus"]=="2"){ ?> selected="selected" <? }?>>Usuaris</option>
	</select><? }?>
	<input type="submit" name="Modifica" value="Modifica">
	</form>
<? 
}
 mysql_close();?>
</body>
</html>